package com.example.xianji.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.adapter.Province_Adapter;
import com.example.xianji.adapter.ZhaoPing_Adapter;
import com.example.xianji.bean.YiJi_Class;
import com.example.xianji.bean.ZhaoPing;
import com.example.xianji.widgets.CustomProgressDialog;
import com.example.xianji.widgets.MyListview;
import com.example.xianji.widgets.PullToRefreshView;
import com.example.xianji.widgets.PullToRefreshView.OnFooterRefreshListener;
import com.example.xianji.widgets.PullToRefreshView.OnHeaderRefreshListener;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.PopupWindow.OnDismissListener;

public class Main_ZhaoPing_DaTingActivity extends Activity implements OnClickListener, OnHeaderRefreshListener, OnFooterRefreshListener {
    public static String zhaoping_province_name = "地区", zhaoping_province_id, zhaoping_hangye_fenlei_name = "行业分类", zhaoping_hangye_id,
            zhaoping_zhiwei_name = "职位类别", zhaoping_zhiwei_id;
    public static boolean zhaoping_province_flag = false, zhaoping_hangye_flag = false, zhaoping_zhiwei_flag = false;
    private ImageView zhaoping_dating_back, zhaoping_search;
    private PullToRefreshView mPullToRefreshView;
    private ZhaoPing_Adapter mZhaoPing_Adapter;
    private ArrayList<Integer> data;
    private MyListview mMyListview;
    private RelativeLayout zhiwei_leibie;
    private int page = 1;
    private ArrayList<ZhaoPing> list = new ArrayList<ZhaoPing>();
    private CustomProgressDialog mCustomProgressDialog;
    private LinearLayout layout1, linear_layout_zhaoping_title;
    private PopupWindow show_popupWindow;
    private GridView province_gridview;
    private Province_Adapter mProvince_Adapter, hangye_Adapter, zhiwei_Adapter;
    private ArrayList<YiJi_Class> province_list = new ArrayList<YiJi_Class>();
    private ArrayList<YiJi_Class> zhaoping_hangye_list = new ArrayList<YiJi_Class>();
    private ArrayList<YiJi_Class> zhaoping_zhiwei_list = new ArrayList<YiJi_Class>();
    private RelativeLayout relativelayout_zhaoping_place, relativelayout_zhaoping_hangye;
    private TextView text_zhaoping_province_name, text_hangye_fenlei, text_hangye_zhiwei, text_zhaoping_title;
    //    private SharedPreferences sp;
//    private Editor ed;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                mMyListview.setAdapter(mZhaoPing_Adapter);
                mPullToRefreshView.onHeaderRefreshComplete();
            } else if (msg.what == 2) {
                mZhaoPing_Adapter.notifyDataSetChanged();
                mPullToRefreshView.onFooterRefreshComplete();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main__zhao_ping__da_ting);
        //XJApplication.getInstance3().addActivity3(this);
        zhaoping_dating_back = (ImageView) findViewById(R.id.zhaoping_dating_back);

        mPullToRefreshView = (PullToRefreshView) findViewById(R.id.pullToRefreshView);
        mMyListview = (MyListview) findViewById(R.id.my_listview);
        zhiwei_leibie = (RelativeLayout) findViewById(R.id.zhiwei_leibie);
        zhaoping_search = (ImageView) findViewById(R.id.zhaoping_search);
        mZhaoPing_Adapter = new ZhaoPing_Adapter(Main_ZhaoPing_DaTingActivity.this, list);
        relativelayout_zhaoping_place = (RelativeLayout) findViewById(R.id.relativelayout_zhaoping_place);
        linear_layout_zhaoping_title = (LinearLayout) findViewById(R.id.linear_layout_zhaoping_title);
        text_zhaoping_province_name = (TextView) findViewById(R.id.text_zhaoping_province_name);
        relativelayout_zhaoping_hangye = (RelativeLayout) findViewById(R.id.relativelayout_zhaoping_hangye);
        text_hangye_fenlei = (TextView) findViewById(R.id.text_hangye_fenlei);
        text_hangye_zhiwei = (TextView) findViewById(R.id.text_hangye_zhiwei);
        text_zhaoping_title = (TextView) findViewById(R.id.text_zhaoping_title);

        text_zhaoping_province_name.setText(zhaoping_province_name);

//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();

        if (Main_SearchActivity.text_zhaoping_content.length() > 0) {
            text_zhaoping_title.setText(Main_SearchActivity.text_zhaoping_content);
        } else {
            text_zhaoping_title.setText("招聘大厅");
        }

        text_zhaoping_province_name.setText(zhaoping_province_name);
        text_hangye_fenlei.setText(zhaoping_hangye_fenlei_name);
        text_hangye_zhiwei.setText(zhaoping_zhiwei_name);
        get_qiye_data();

        zhaoping_dating_back.setOnClickListener(this);
        mPullToRefreshView.setOnHeaderRefreshListener(this);
        mPullToRefreshView.setOnFooterRefreshListener(this);
        zhiwei_leibie.setOnClickListener(this);
        zhaoping_search.setOnClickListener(this);
        relativelayout_zhaoping_place.setOnClickListener(this);
        relativelayout_zhaoping_hangye.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.zhaoping_dating_back) {
            this.finish();
        } else if (v.getId() == R.id.zhiwei_leibie) {
            province_Photo_show();
            zhiwei_Adapter = new Province_Adapter(this, zhaoping_zhiwei_list);
            province_gridview.setNumColumns(3);
            province_gridview.setAdapter(zhiwei_Adapter);
            show_popupWindow.showAsDropDown(linear_layout_zhaoping_title);

            province_gridview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {
                    text_hangye_zhiwei.setText(zhaoping_zhiwei_list.get(arg2).getName());
                    zhaoping_zhiwei_id = zhaoping_zhiwei_list.get(arg2).getId() + "";
                    zhaoping_zhiwei_name = province_list.get(arg2).getName();
                    if (show_popupWindow != null) {
                        show_popupWindow.dismiss();
                        show_popupWindow = null;
                    }
                    zhaoping_zhiwei_flag = true;
                    mCustomProgressDialog = CustomProgressDialog.createDialog(Main_ZhaoPing_DaTingActivity.this);
                    mCustomProgressDialog.setCanceledOnTouchOutside(false);
                    mCustomProgressDialog.show();
                    get_qiye_data();
                }
            });
        } else if (v.getId() == R.id.zhaoping_search) {
            Intent intent = new Intent(Main_ZhaoPing_DaTingActivity.this, Main_SearchActivity.class);
            intent.putExtra("search", 3);
            startActivity(intent);
        } else if (v.getId() == R.id.relativelayout_zhaoping_place) {
            province_Photo_show();
            mProvince_Adapter = new Province_Adapter(Main_ZhaoPing_DaTingActivity.this, province_list);
            province_gridview.setNumColumns(4);
            province_gridview.setAdapter(mProvince_Adapter);
            show_popupWindow.showAsDropDown(linear_layout_zhaoping_title);

            province_gridview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {
                    text_zhaoping_province_name.setText(province_list.get(arg2).getName());
                    zhaoping_province_name = province_list.get(arg2).getName();
                    zhaoping_province_id = province_list.get(arg2).getId() + "";
                    if (show_popupWindow != null) {
                        show_popupWindow.dismiss();
                        show_popupWindow = null;
                    }
                    zhaoping_province_flag = true;
                    mCustomProgressDialog = CustomProgressDialog.createDialog(Main_ZhaoPing_DaTingActivity.this);
                    mCustomProgressDialog.setCanceledOnTouchOutside(false);
                    mCustomProgressDialog.show();
                    get_qiye_data();
                }
            });
        } else if (v.getId() == R.id.relativelayout_zhaoping_hangye) {
            province_Photo_show();
            hangye_Adapter = new Province_Adapter(this, zhaoping_hangye_list);
            province_gridview.setNumColumns(3);
            province_gridview.setAdapter(hangye_Adapter);
            show_popupWindow.showAsDropDown(linear_layout_zhaoping_title);
            province_gridview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {
                    text_hangye_fenlei.setText(zhaoping_hangye_list.get(arg2).getName());
                    if (show_popupWindow != null) {
                        show_popupWindow.dismiss();
                        show_popupWindow = null;
                    }
                    zhaoping_hangye_fenlei_name = zhaoping_hangye_list.get(arg2).getName();
                    zhaoping_hangye_id = zhaoping_hangye_list.get(arg2).getId() + "";
                    zhaoping_hangye_flag = true;
                    mCustomProgressDialog = CustomProgressDialog.createDialog(Main_ZhaoPing_DaTingActivity.this);
                    mCustomProgressDialog.setCanceledOnTouchOutside(false);
                    mCustomProgressDialog.show();
                    get_qiye_data();
                }
            });
        }
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        page++;
        get_qiye_data_more();
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        page = 1;
        get_qiye_data();
    }

    /**
     * 招聘大厅
     */
    public void get_qiye_data() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("page", 1 + "");
        map.put("k", Main_SearchActivity.text_zhaoping_content);
        if (zhaoping_province_flag) {
            map.put("areaid", zhaoping_province_id);
        }
        if (zhaoping_hangye_flag) {
            map.put("cid", zhaoping_hangye_id);
        }

        if (zhaoping_zhiwei_flag) {
            map.put("sjob", zhaoping_zhiwei_id);
        }
        JSONObject params = new JSONObject(map);

        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.zhaoping,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        list.clear();
                        JSONArray array = response.getJSONArray("data");
                        JSONArray array3 = array.getJSONArray(3);
                        JSONArray array2 = array.getJSONArray(2);
                        JSONArray array1 = array.getJSONArray(1);
                        JSONArray array0 = array.getJSONArray(0);
                        if (array3.length() > 0) {
                            for (int i = 0; i < array3.length(); i++) {
                                JSONObject object = array3.getJSONObject(i);
                                ZhaoPing mZhaoPing = new ZhaoPing();
                                mZhaoPing.setCom_name(object.getString("com_name"));
                                mZhaoPing.setCity(object.getJSONObject("jcity").getString("name"));
                                mZhaoPing.setCreated_at(object.getString("created_at"));
                                mZhaoPing.setJob_title(object.getString("job_title"));
                                mZhaoPing.setPrivonce(object.getJSONObject("jprovince").getString("name"));
                                mZhaoPing.setJob_education(object.getInt("job_education"));
                                mZhaoPing.setJob_salary(object.getInt("job_salary"));
                                mZhaoPing.setId(object.getString("id"));
                                list.add(mZhaoPing);
                            }
                        } else {
                            Toast.makeText(Main_ZhaoPing_DaTingActivity.this, "暂无数据", Toast.LENGTH_LONG).show();
                        }
                        province_list.clear();
                        for (int i = 0; i < array0.length(); i++) {
                            JSONObject obj0 = array0.getJSONObject(i);
                            YiJi_Class mYiJi_Class = new YiJi_Class();
                            mYiJi_Class.setId(obj0.getInt("id"));
                            mYiJi_Class.setName(obj0.getString("name"));
                            province_list.add(mYiJi_Class);
                        }
                        zhaoping_hangye_list.clear();
                        for (int i = 0; i < array1.length(); i++) {
                            JSONObject obj1 = array1.getJSONObject(i);
                            YiJi_Class mYiJi_Class = new YiJi_Class();
                            mYiJi_Class.setId(obj1.getInt("id"));
                            mYiJi_Class.setName(obj1.getString("cat_name"));
                            zhaoping_hangye_list.add(mYiJi_Class);
                        }
                        zhaoping_zhiwei_list.clear();
                        for (int i = 0; i < array2.length(); i++) {
                            JSONObject obj2 = array2.getJSONObject(i);
                            YiJi_Class mYiJi_Class = new YiJi_Class();
                            mYiJi_Class.setId(obj2.getInt("id"));
                            mYiJi_Class.setName(obj2.getString("cat_name"));
                            zhaoping_zhiwei_list.add(mYiJi_Class);
                        }
                        handler.sendEmptyMessage(1);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                mPullToRefreshView.onHeaderRefreshComplete();
                if (mCustomProgressDialog != null) {
                    mCustomProgressDialog.dismiss();
                    mCustomProgressDialog = null;
                }
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }


    public void get_qiye_data_more() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("page", page + "");
        map.put("k", Main_SearchActivity.text_zhaoping_content);
        if (zhaoping_province_flag) {
            map.put("areaid", zhaoping_province_id);
        }
        if (zhaoping_hangye_flag) {
            map.put("cid", zhaoping_hangye_id);
        }
        if (zhaoping_zhiwei_flag) {
            map.put("sjob", zhaoping_zhiwei_id);
        }
        JSONObject params = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.zhaoping,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        JSONArray array = response.getJSONArray("data");
                        JSONArray array2 = array.getJSONArray(0);
                        if (array2.length() > 0) {
                            for (int i = 0; i < array2.length(); i++) {
                                JSONObject object = array2.getJSONObject(i);
                                ZhaoPing mZhaoPing = new ZhaoPing();
                                mZhaoPing.setCom_name(object.getString("com_name"));
                                mZhaoPing.setCity(object.getJSONObject("jcity").getString("name"));
                                mZhaoPing.setCreated_at(object.getString("created_at"));
                                mZhaoPing.setJob_title(object.getString("job_title"));
                                mZhaoPing.setPrivonce(object.getJSONObject("jprovince").getString("name"));
                                mZhaoPing.setJob_education(object.getInt("job_education"));
                                mZhaoPing.setJob_salary(object.getInt("job_salary"));
                                mZhaoPing.setId(object.getString("id"));
                                list.add(mZhaoPing);
                            }
                        } else {
                            Toast.makeText(Main_ZhaoPing_DaTingActivity.this, "没有更多数据", Toast.LENGTH_LONG).show();
                        }
                        handler.sendEmptyMessage(2);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                mPullToRefreshView.onFooterRefreshComplete();
                if (mCustomProgressDialog != null) {
                    mCustomProgressDialog.dismiss();
                    mCustomProgressDialog = null;
                }
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    private void province_Photo_show() {
        layout1 = (LinearLayout) LayoutInflater.from(
                Main_ZhaoPing_DaTingActivity.this).inflate(
                R.layout.province_popoupwindow_listview, null);
        province_gridview = (GridView) layout1.findViewById(R.id.province_gridview);
        province_gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
        show_popupWindow = new PopupWindow(layout1, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        show_popupWindow.setBackgroundDrawable(new BitmapDrawable());


        show_popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        show_popupWindow.setAnimationStyle(R.style.AnimTopMiddle);
        show_popupWindow.update();
        show_popupWindow.setOutsideTouchable(true);
        show_popupWindow.setFocusable(true);
        show_popupWindow.setContentView(layout1);

        show_popupWindow.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss() {
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Main_SearchActivity.text_zhaoping_content.length() > 0) {
            text_zhaoping_title.setText(Main_SearchActivity.text_zhaoping_content);
        } else {
            text_zhaoping_title.setText("招聘大厅");
        }

        if (Main_SearchActivity.zhaoping_search_id == 1) {
            get_qiye_data();
            Main_SearchActivity.zhaoping_search_id = 0;
        }
    }
}